// 组件数据格式化为接口需要的ids
export const formatToIds = (val = []) => val?.join(',') || ''

// 接口需要ids格式化为组件数据
export const formatToSel = (val) => (val?.length ? val.split(',') : [])

// 处理返回数据的合并单元格 判断一个key下连续相同的value 返回起始index和连续个数
export const findConsecutiveObjects = (arr, key) => {
  const result = []
  let consecutiveCount = 1
  let startIndex = 0
  let prevName = null

  for (let i = 0; i < arr.length; i++) {
    const currentName = arr[i][key]
    if (currentName === prevName) {
      consecutiveCount++
    } else {
      if (consecutiveCount > 1) {
        // 如果连续数量大于1，则记录前一个连续序列
        result.push({
          startIndex: startIndex,
          count: consecutiveCount,
        })
      }
      // 重置连续计数器和起始索引
      consecutiveCount = 1
      startIndex = i
    }
    prevName = currentName
  }

  // 检查最后一个连续序列
  if (consecutiveCount > 1) {
    result.push({
      startIndex: startIndex,
      count: consecutiveCount,
    })
  }

  return result
}

// 处理合并单元格对象扁平化
export const flatMapArr = (arr) => {
  const flattenedArray = arr.reduce((acc, item) => {
    return acc.concat(
      item.newFeiShuUserDtoList.map((userDto) => {
        delete item.newFeiShuUserDtoList
        return {
          deptId: item.deptId,
          deptName: item.name,
          ...userDto, // 将 userDto 的属性复制到新对象中
        }
      })
    )
  }, [])
  const todoList = flattenedArray.reduce((acc, item) => {
    if (item.newProjectManagementDtoList.length !== 0) {
      return acc.concat(
        item.newProjectManagementDtoList.map((todo) => {
          delete item.newProjectManagementDtoList
          return {
            ...todo,
            ...item,
          }
        })
      )
    } else {
      return [...acc, item]
    }
  }, [])

  return todoList
}
